package cn.tlb.xutilsdownload.annotation;

import android.app.Activity;

import java.lang.reflect.Field;

/**
 * Created by 汤林冰 on 2015/11/25 14:18.
 */
public class CompomentUtil {
//    private static class CompomentUtilFactory{
//        static CompomentUtil util = new CompomentUtil();
//    }
    private CompomentUtil(){}

    public static void inject(Activity activity){
        Class<? extends Activity> aClass = activity.getClass();
        InjectContent injectContent = aClass.getAnnotation(InjectContent.class);
        if(injectContent != null){
            activity.setContentView(injectContent.value());
        }

        Field[] fields = aClass.getDeclaredFields();
        if(fields == null) return;
        for (Field field : fields){
            InjectView annotation = field.getAnnotation(InjectView.class);
            if(annotation != null){
                boolean accessible = field.isAccessible();
                field.setAccessible(true);
                try {
                    field.set(activity, (activity.findViewById(annotation.value())));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }finally {
                    field.setAccessible(accessible);
                }
            }
        }
    }

}
